package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Message;
import com.example.demo.exception.bizException.BizException;
import com.example.demo.mapper.MessageMapper;
import com.example.demo.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
* <p>
*  服务实现类
* </p>
*
* @author cy
* @since 2020-08-01
*/
@Slf4j
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Override
    public Page<Message> listMessagesByPage(int page, int pageSize, String factor,boolean time,boolean timeAsc) {
        log.info("正在执行分页查询message: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<Message> queryWrapper =  new QueryWrapper<Message>().like("message_title", factor)
                .or().like("message_content",factor)
                .orderBy(time,timeAsc,"time");
        Page<Message> result = super.page(new Page<>(page, pageSize),queryWrapper);
        log.info("分页查询message完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public Message getMessageById(int id) {
        log.info("正在查询message中id为{}的数据",id);
        Message message = super.getById(id);
        log.info("查询id为{}的message{}",id,(null == message?"无结果":"成功"));
        return message;
    }

    @Override
    public int insertMessage(Message message) {
        log.info("正在插入message");
        LocalDateTime localDateTime = LocalDateTime.now();
        message.setTime(localDateTime);
        if (super.save(message)) {
            log.info("插入message成功,id为{}",message.getMessageId());
            return message.getMessageId();
        } else {
            log.error("插入message失败");
            throw new BizException("添加失败");
        }
    }

    @Transactional
    @Override
    public int deleteMessageById(int id) {
        log.info("正在删除id为{}的message",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的message成功",id);
            return id;
        } else {
            log.error("删除id为{}的message失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Transactional
    @Override
    public int updateMessage(Message message) {
        log.info("正在更新id为{}的message",message.getMessageId());
        LocalDateTime localDateTime = LocalDateTime.now();
        message.setTime(localDateTime);
        if (super.updateById(message)) {
            log.info("更新d为{}的message成功",message.getMessageId());
            return message.getMessageId();
        } else {
            log.error("更新id为{}的message失败",message.getMessageId());
            throw new BizException("更新失败[id=" + message.getMessageId() + "]");
        }
    }

}
